#include "ISceneManagement.h"

Scene::ISceneManagement::ISceneManagement()
{
	_mouseRotatesCamera = true; 
	_keysMovesCamera = true;
}
 
Scene::ISceneManagement::~ISceneManagement()
{
	cout <<"_~ISceneManagement()_"<<endl;
}

void Scene::ISceneManagement::setRenderer(SceneRenderer *renderer )
{
	_renderer = renderer;
}


Camera* Scene::ISceneManagement::getCurrentCam() const
{
	return _currentCamera;
}

void Scene::ISceneManagement::MouseMoved (Sint16 xDelta, Sint16 yDelta)
{
	if( _mouseRotatesCamera && _currentCamera != NULL)
	{
		if( xDelta < 0 )
			_currentCamera->ROT_LEFT( xDelta );
		if( xDelta > 0 )
			_currentCamera->ROT_RIGHT( xDelta );
		if( yDelta < 0 )
			_currentCamera->ROT_DOWN( yDelta );
		if( yDelta > 0 )	
			_currentCamera->ROT_UP( yDelta );		
	}	
}

void Scene::ISceneManagement::KeyLoop(Uint8* _keys)
{
	if( _currentCamera == NULL )
		return;
	// rotate cam with keys
	if( _keys[SDLK_LEFT] )
		_currentCamera->ROT_LEFT( -20 );	

	if( _keys[SDLK_UP] )
		_currentCamera->ROT_UP( -20 );

	if( _keys[SDLK_RIGHT] )
		_currentCamera->ROT_RIGHT( 20 );

	if( _keys[SDLK_DOWN] )
		_currentCamera->ROT_DOWN( 20 );
	
	// move cam with keys ( W - A - S - D )
	if( _keysMovesCamera )
	{
		if( _keys['w'] )
		{
			if( _keys['a'] && !_keys['d'])
				_currentCamera->GO_FORWARD_LEFT();
			else if( !_keys['a'] && _keys['d'])
				_currentCamera->GO_FORWARD_RIGHT();	
			else
				_currentCamera->GO_FORWARD();	
		}
		if( _keys['s'] )
		{
			if( _keys['a'] && !_keys['d'])
				_currentCamera->GO_BACKWARD_LEFT();
			else if( !_keys['a'] && _keys['d'])
				_currentCamera->GO_BACKWARD_RIGHT();	
			else
				_currentCamera->GO_BACKWARD();		
		}
		if( _keys['a'] )
		{
			if( !_keys['s'] && !_keys['w'] )
				_currentCamera->STRAFE_LEFT();   		
		}
		if( _keys['d'] )
		{
			if( !_keys['s'] && !_keys['w'] )
				_currentCamera->STRAFE_RIGHT();
		}   
	}		
}


void Scene::ISceneManagement::AddCamera( Camera *newCamera )
{
	// TODO prüfen ob bereits vorhanden
	if( newCamera != NULL )
		_cameras.push_back( newCamera );
}

void Scene::ISceneManagement::ActivateCam( Camera *cam )
{
	for( list<Camera*>::iterator it = _cameras.begin(); it != _cameras.end();it++ )
	{
		if( (*it) == cam )
			_currentCamera = cam;
	}
}

void Scene::ISceneManagement::RenderObject ()
{
//Utils::PRINT_ELAPSED_TIME( ">> TIME_ELAPSED_FOR_CPU: ", 30 );	
	//long time =SDL_GetTicks();
	//cout <<"RenderObject1"<<endl;
	// AT FIRST RENDER THE ACTIVATED CAMERA
	_currentCamera->RenderObject();
	//cout <<"1time_ms: "<<SDL_GetTicks() - time<<endl;
	//time =SDL_GetTicks();
	//cout <<"RenderObject2"<<endl;
	// THAN RENDER PRE OBJECTS
	RenderBeforeBase();
	//cout <<"2time_ms: "<<SDL_GetTicks() - time<<endl;
	//time =SDL_GetTicks();
	//cout <<"RenderObject3"<<endl;
	// HERE WE WILL RENDER THE MAIN SCENE OBJECTS
	RenderChildObjects ();
	//cout <<"3time_ms: "<<SDL_GetTicks() - time<<endl;
	//time =SDL_GetTicks();
	//cout <<"RenderObject4"<<endl;
	// AND LAST BUT NOT LEAST WE WILL RENDER POST OBJECTS
	RenderAfterBase();	
	//cout <<"4time_ms: "<<SDL_GetTicks() - time<<endl;
	//time =SDL_GetTicks();
	//cout <<"RenderObject5"<<endl;
//Utils::PRINT_ELAPSED_TIME( ">> TIME_ELAPSED_FOR_GPU: ", 30 );	
}

void Scene::ISceneManagement::WriteString (int xPos, int yPos, string str)
{
	_renderer->enter2DMode ();
	_renderer->renderBitmapString ( (float)xPos,(float)yPos, 0.0,GLUT_BITMAP_TIMES_ROMAN_10,str);
	_renderer->leave2DMode ();
}



